<?php

namespace App\Models\Factory\V0;

use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Orm\DataDeliveryDownloadLog;
use App\Models\Orm\DataDeliveryUploadLog;
use App\Models\Orm\DataProductApplyLog;
use App\Models\Orm\V0\Delivery;
use App\Models\Orm\DeliveryCount;
use App\Helpers\V0\SLogger;
use App\Models\Orm\V0\DeliveryLog;
use App\Models\Orm\UserDelivery;

/**
 * 渠道数据统计
 */
class DeliveryFactory extends AbsModelFactory
{

    /**
     * 添加渠道统计流水数据
     * @param $channerl_fr
     * @param $userId ,
     */
    public static function insertDeliverylLog($channel)
    {

        $channels = DeliveryFactory::getDeliveryByNid($channel['nid']);


        if(!$channels)
        {
            SLogger::getStream()->error('渠道有误！');

            return false;

        }

        if($channel['userphone']){

            $userId = UserFactory::getUserByMobile($channel['userphone'])->id;
        }else{
            $userId = 0;
        }
        //拿到用户的id


//        //首先判断是否存在这条数据
//        $result = DeliveryLog::where('user_id',$userId)->first();
//        if($result)
//        {
//            return false;
//        }


        //渠道id
        $channel_id = $channels ->id;

        $delivery['channel_id'] = $channel_id;
        $delivery['create_time'] = time();
        $delivery['type'] = $channel['type'];
        $delivery['channel_nid'] = isset($channel['nid']) ? $channel['nid'] : 'H5';
        $delivery['user_id'] = $userId;
        $delivery['create_date'] = date('Y-m-d H:i:s',time());
        $delivery['create_ip'] = Utils::ipAddress();
        $result = DeliveryLog::create($delivery);
        return $result ? true :false;

    }

    /** 通过渠道标识nid获取渠道数据
     * @param $nid
     */

    public static function getDeliveryByNid($nid)
    {

        $channelId = \App\Models\Orm\Delivery::where('nid', '=', $nid)->where('status',1)->first();
        return $channelId ? $channelId : '';
    }

    /**
     * 注册时渠道统计---更新统计主表中总数据
     * @param $register
     * @param int $userId
     */
    public static function updateDeliveryRegisterCount($channelNid)
    {

        $re = Delivery::where('nid', '=', $channelNid)->first();
        if(!$re)
        {
            SLogger::getStream()->error('渠道有误！');
            return false;
        }
        $re->register += 1;
        $re->updated_at = date('Y-m-d H:i:s', time());
        if ($re->save())
        {
            SLogger::getStream()->info('注册渠道统计成功！');
            return true;
        }
        else
        {
            SLogger::getStream()->error('注册渠道统计失败！');
            return false;
        }
    }

    /**
     * 安卓渠道APP下载统计
     *
     * @param $channel_id
     * @param int $channel_nid
     */
    public static function createDeliverUpload($channel_id, $channel_nid = 0)
    {
        $insert_data = [
            'channel_id' => $channel_id,
            'channel_nid' => $channel_nid,
            'create_time' => time(),
            'type' => 4,
            'create_ip' => strval(ip2long(Utils::ipAddress())),
            'create_date' => date("Y-m-d H:i:s")
        ];

        DeliveryLog::insert($insert_data);
    }
}
